﻿using System;
using System.Collections.Generic;
using System.IO;
using ProjScan.Config;
using ProjScan.Config.GUIConfig;
using ProjScan.ScanGUI;
using ProjScan.ScanRule.RuleData;
using ProjScan.Tools;
using UnityEditor;
using UnityEngine;

namespace ProjScan.Modules
{
	// Token: 0x0200030E RID: 782
	internal class ModulesManager : Singleton<ModulesManager>
	{
		// Token: 0x0600126A RID: 4714 RVA: 0x000564F0 File Offset: 0x000546F0
		public bool CheckIsCanRun()
		{
			bool flag = !this.ProjIsReady();
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				this.CreateScanDir();
				FixLog.Instance.OpenFixLog(false);
				ScanLog.Instance.OpenScanLog(false);
				ScanLog.Instance.LogInfo("Output : " + ToolsHelper.GetScanOutPut());
				this.Init();
				Singleton<ScanOverviewManager>.Instance.Init();
				this.streamWriter_rules = Singleton<FileStreamFactory>.Instance.GetStreamWriter(ToolsHelper.GetScanOutPut() + "/rules.csv", false);
				this.streamWriter_rules.WriteLine("RuleName,SceneId,Result,TableTag,DataId");
				this.streamWriter_thres = Singleton<FileStreamFactory>.Instance.GetStreamWriter(ToolsHelper.GetScanOutPut() + "/thres.csv", false);
				this.streamWriter_thres.WriteLine("RuleName,Path(Regex),Value,Priority");
				this.globalConfig = (Singleton<ConfigMgr>.Instance.GetConfig<GlobalConfig>() as GlobalConfig);
				result = true;
			}
			return result;
		}

		// Token: 0x0600126B RID: 4715 RVA: 0x000565DC File Offset: 0x000547DC
		public void Init()
		{
			Singleton<ScanInfo>.Instance.Init();
			ToolsHelper.ShowProgressBar("Manager", "Initializing tools...", 0.1f, false);
			Singleton<RuleDataManager>.Instance.Init();
			Singleton<ConfigMgr>.Instance.InitConfig();
			Singleton<ConfigMgr>.Instance.LoadConfig();
			ApiCompatibilityUtils.Instance.Reset();
			AssetPathManager.Instance.Init();
			WhiteAssetManager.Instance.Init();
			this.InitFailedResultManager();
			Singleton<RuleInfoManager>.Instance.Init();
			Singleton<RuleInfoManager>.Instance.LoadRuleInfo();
		}

		// Token: 0x0600126C RID: 4716 RVA: 0x0005666C File Offset: 0x0005486C
		public bool InitFailedResultManager()
		{
			FailedResultManager.Instance.Init();
			FailedResultManager.Instance.LoadAll();
			return true;
		}

		// Token: 0x0600126D RID: 4717 RVA: 0x00056698 File Offset: 0x00054898
		private void SaveModuleStates(Dictionary<string, bool> modulesToRun)
		{
			int value = 0;
			int num = 0;
			this.SetState(ref value, num++, modulesToRun["Basic_Assets_Check"]);
			this.SetState(ref value, num++, modulesToRun["Scenes_Check"]);
			this.SetState(ref value, num++, modulesToRun["Global_Settings_Check"]);
			this.SetState(ref value, num++, modulesToRun["CS_Check"]);
			this.SetState(ref value, num++, modulesToRun["Custom_Rules_Check"]);
			this.SetState(ref value, num++, modulesToRun["Effects_Play_Check"]);
			this.SetState(ref value, num++, modulesToRun["Shader_Analysis"]);
			this.SetState(ref value, num++, modulesToRun["Art_Assets_Check"]);
			ProjectSpecificEditorPrefs.SetInt(this.modulesToRunPrefsKey, value);
		}

		// Token: 0x0600126E RID: 4718 RVA: 0x00056780 File Offset: 0x00054980
		private Dictionary<string, bool> LoadModuleStates()
		{
			int @int = ProjectSpecificEditorPrefs.GetInt(this.modulesToRunPrefsKey, 0);
			Dictionary<string, bool> dictionary = new Dictionary<string, bool>();
			int num = 0;
			dictionary["Basic_Assets_Check"] = this.GetState(@int, num++);
			dictionary["Scenes_Check"] = this.GetState(@int, num++);
			dictionary["Global_Settings_Check"] = this.GetState(@int, num++);
			dictionary["CS_Check"] = this.GetState(@int, num++);
			dictionary["Custom_Rules_Check"] = this.GetState(@int, num++);
			dictionary["Effects_Play_Check"] = this.GetState(@int, num++);
			dictionary["Shader_Analysis"] = this.GetState(@int, num++);
			dictionary["Art_Assets_Check"] = this.GetState(@int, num++);
			return dictionary;
		}

		// Token: 0x0600126F RID: 4719 RVA: 0x00056868 File Offset: 0x00054A68
		private void SetState(ref int state, int bitPosition, bool enabled)
		{
			if (enabled)
			{
				state |= 1 << bitPosition;
			}
			else
			{
				state &= ~(1 << bitPosition);
			}
		}

		// Token: 0x06001270 RID: 4720 RVA: 0x00056898 File Offset: 0x00054A98
		private bool GetState(int state, int bitPosition)
		{
			bool isScanQuit = Singleton<ScanInfo>.Instance.isScanQuit;
			return isScanQuit || (state & 1 << bitPosition) != 0;
		}

		// Token: 0x06001271 RID: 4721 RVA: 0x000568C8 File Offset: 0x00054AC8
		public void Run(Dictionary<string, bool> modulesToRun)
		{
			this.SaveModuleStates(modulesToRun);
			ProjectSpecificEditorPrefs.SetDateTime("ScanStartTime", DateTime.Now);
			Singleton<PerformanceMeasurer>.Instance.StartCheckpointMeasure();
			Singleton<PerformanceMeasurer>.Instance.AddExecutionCheckpoint("BeginToRun");
			Singleton<PerformanceMeasurer>.Instance.RecordMaxValues();
			Singleton<PerformanceMeasurer>.Instance.PushState("Manager");
			try
			{
				bool flag = modulesToRun["Basic_Assets_Check"] || modulesToRun["CS_Check"] || modulesToRun["Shader_Analysis"] || modulesToRun["Art_Assets_Check"] || modulesToRun["Effects_Play_Check"];
				if (flag)
				{
					AssetPathManager.Instance.FindAssetPath(false);
					AssetPathManager.Instance.WriterDirMap();
				}
			}
			catch (Exception ex)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Manager, "AssetPathManager\n", string.Concat(new string[]
				{
					(ex.GetType() == null) ? null : ex.GetType().ToString(),
					"\n",
					ex.Message,
					"\n",
					ex.StackTrace
				}));
			}
			try
			{
				bool flag2 = modulesToRun["Basic_Assets_Check"];
				if (flag2)
				{
					Singleton<BasicAssetsCheckModule>.Instance.Run(this.streamWriter_rules, this.streamWriter_thres);
				}
				this.streamWriter_rules.Flush();
				bool flag3 = modulesToRun["Scenes_Check"];
				if (flag3)
				{
					Singleton<ScenesCheckModule>.Instance.Run(this.streamWriter_rules);
				}
				this.streamWriter_rules.Flush();
				bool flag4 = modulesToRun["Global_Settings_Check"];
				if (flag4)
				{
					Singleton<GlobalSettingsCheckModule>.Instance.Run(this.streamWriter_rules);
				}
				this.streamWriter_rules.Flush();
				bool flag5 = modulesToRun["CS_Check"];
				if (flag5)
				{
					Singleton<CSCheckModule>.Instance.Run(this.streamWriter_rules, this.streamWriter_thres);
				}
				this.streamWriter_rules.Flush();
				bool flag6 = modulesToRun["Custom_Rules_Check"];
				if (flag6)
				{
					Singleton<CustomRulesCheckModule>.Instance.Run(this.streamWriter_rules);
				}
				this.streamWriter_rules.Flush();
				bool flag7 = modulesToRun["Shader_Analysis"];
				if (flag7)
				{
					Singleton<ShaderAnalysisCheckModule>.Instance.Run(this.streamWriter_rules, this.streamWriter_thres);
				}
				this.streamWriter_rules.Flush();
				bool flag8 = modulesToRun["Art_Assets_Check"];
				if (flag8)
				{
					Singleton<ArtAssetsCheckModule>.Instance.Run(this.streamWriter_rules, this.streamWriter_thres);
				}
				this.streamWriter_rules.Flush();
				Singleton<PerformanceMeasurer>.Instance.CloseAndRecordState();
				Singleton<PerformanceMeasurer>.Instance.FinalizeCheckpointMeasure();
				Singleton<PerformanceMeasurer>.Instance.WritePerformanceDataToFile();
				bool flag9 = modulesToRun["Effects_Play_Check"] && Singleton<ScanInfo>.Instance.isRunning;
				if (flag9)
				{
					Singleton<EffectsPlayCheckModule>.Instance.Run(this.streamWriter_rules);
				}
			}
			catch (Exception ex2)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Manager, ex2.GetType().Name, string.Concat(new string[]
				{
					(ex2.GetType() == null) ? null : ex2.GetType().ToString(),
					"\n",
					ex2.Message,
					"\n",
					ex2.StackTrace
				}));
			}
			finally
			{
				bool flag10 = !modulesToRun["Effects_Play_Check"];
				if (flag10)
				{
					this.Close();
					this.InitFailedResultManager();
					this.OutputScanOverResult();
					bool flag11 = !Singleton<ScanInfo>.Instance.isScanQuit;
					if (flag11)
					{
						this.OpenScanOverviewWindow();
					}
					this.CheckQuit();
				}
			}
		}

		// Token: 0x06001272 RID: 4722 RVA: 0x00056CB8 File Offset: 0x00054EB8
		public void OutputScanOverResult()
		{
			ScanLog.UnityLog("输出检测结果");
			Dictionary<string, bool> modulesToRun = this.LoadModuleStates();
			Singleton<ScanOverviewManager>.Instance.OutputScanResultOverviewCSV(modulesToRun);
		}

		// Token: 0x06001273 RID: 4723 RVA: 0x00008A2B File Offset: 0x00006C2B
		public void OpenScanOverviewWindow()
		{
			ScanLog.UnityLog("打开检测结果界面");
			ScanOverviewWindow.ShowWindow();
		}

		// Token: 0x06001274 RID: 4724 RVA: 0x00056CE4 File Offset: 0x00054EE4
		public void Fix(Dictionary<string, bool> modulesToFix)
		{
			Singleton<PerformanceMeasurer>.Instance.StartCheckpointMeasure();
			Singleton<PerformanceMeasurer>.Instance.AddExecutionCheckpoint("BeginToFix");
			Singleton<PerformanceMeasurer>.Instance.RecordMaxValues();
			Singleton<PerformanceMeasurer>.Instance.PushState("Manager");
			try
			{
				bool flag = modulesToFix["Basic_Assets_Fix"];
				if (flag)
				{
					Singleton<BasicAssetsFixModule>.Instance.Fix();
				}
				bool flag2 = modulesToFix["Scenes_Fix"];
				if (flag2)
				{
					Singleton<ScenesFixModule>.Instance.Fix();
				}
				bool flag3 = modulesToFix["Global_Settings_Fix"];
				if (flag3)
				{
					Singleton<GlobalSettingsFixModule>.Instance.Fix();
				}
				bool flag4 = !modulesToFix["CS_Fix"];
				if (flag4)
				{
				}
				bool flag5 = !modulesToFix["Custom_Rules_Fix"];
				if (flag5)
				{
				}
				bool flag6 = !modulesToFix["Shader_Fix"];
				if (flag6)
				{
				}
				bool flag7 = !modulesToFix["Art_Assets_Fix"];
				if (flag7)
				{
				}
				this.streamWriter_rules.Flush();
				bool flag8 = !modulesToFix["Assets_Reference_Fix"];
				if (flag8)
				{
				}
				this.Close();
			}
			catch (Exception ex)
			{
				this.Close();
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Manager, "AssetPathManager\n", string.Concat(new string[]
				{
					(ex.GetType() == null) ? null : ex.GetType().ToString(),
					"\n",
					ex.Message,
					"\n",
					ex.StackTrace
				}));
			}
		}

		// Token: 0x06001275 RID: 4725 RVA: 0x00056E80 File Offset: 0x00055080
		public static void SingleFix(Module module, string ruleId)
		{
			switch (module)
			{
			case Module.Basic_Assets_Fix:
				Singleton<BasicAssetsFixModule>.Instance.FixSingle(ruleId);
				break;
			case Module.Scenes_Fix:
				Singleton<ScenesFixModule>.Instance.FixSingle(ruleId);
				break;
			case Module.Global_Settings_Fix:
				Singleton<GlobalSettingsFixModule>.Instance.FixSingle(ruleId);
				break;
			case Module.CS_Fix:
				Singleton<CSFixModule>.Instance.FixSingle(ruleId);
				break;
			default:
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, Module.Unknown, "SingleFix", string.Format("moduleType [{0}] not Implemented", module));
				break;
			}
		}

		// Token: 0x06001276 RID: 4726 RVA: 0x00056F0C File Offset: 0x0005510C
		public void Close()
		{
			ScanLog.UnityLog("关闭资源检测");
			Singleton<FileStreamFactory>.Instance.CloseAllStreams();
			ScanLog.Instance.CloseScanLog();
			FixLog.Instance.CloseFixLog();
			bool isEditor = Singleton<ScanInfo>.Instance.isEditor;
			if (isEditor)
			{
				EditorUtility.ClearProgressBar();
			}
			AssetDatabase.Refresh();
		}

		// Token: 0x06001277 RID: 4727 RVA: 0x00056F64 File Offset: 0x00055164
		public void CheckQuit()
		{
			DateTime dateTime = ProjectSpecificEditorPrefs.GetDateTime("ScanStartTime", default(DateTime));
			TimeSpan timeSpan = DateTime.Now - dateTime;
			bool isScanQuit = Singleton<ScanInfo>.Instance.isScanQuit;
			if (isScanQuit)
			{
				ScanLog.UnityLog(string.Format("扫描耗时为 : {0} ms", (int)timeSpan.TotalMilliseconds));
				ScanLog.UnityLog("命令行扫描模式下，扫描完成，退出Unity");
				EditorApplication.Exit(0);
			}
		}

		// Token: 0x06001278 RID: 4728 RVA: 0x00056FD4 File Offset: 0x000551D4
		public Dictionary<string, bool> GetScanModules()
		{
			return new Dictionary<string, bool>(this.globalConfig.scanModules);
		}

		// Token: 0x06001279 RID: 4729 RVA: 0x00056FF8 File Offset: 0x000551F8
		public Dictionary<string, bool> GetFixModules()
		{
			return new Dictionary<string, bool>(this.globalConfig.fixModules);
		}

		// Token: 0x0600127A RID: 4730 RVA: 0x0005701C File Offset: 0x0005521C
		private void CreateScanDir()
		{
			bool flag = Directory.Exists(ToolsHelper.GetScanOutPut());
			if (flag)
			{
				DirectoryInfo directoryInfo = new DirectoryInfo(ToolsHelper.GetScanOutPut());
				foreach (FileInfo fileInfo in directoryInfo.GetFiles())
				{
					bool flag2 = fileInfo.Name != "ResultOverview.csv";
					if (flag2)
					{
						fileInfo.Delete();
					}
				}
				foreach (DirectoryInfo directoryInfo2 in directoryInfo.GetDirectories())
				{
					bool flag3 = directoryInfo2.Name != "FixLog";
					if (flag3)
					{
						Directory.Delete(directoryInfo2.FullName, true);
					}
				}
			}
			else
			{
				Directory.CreateDirectory(ToolsHelper.GetScanOutPut());
			}
		}

		// Token: 0x0600127B RID: 4731 RVA: 0x000570E4 File Offset: 0x000552E4
		private bool ProjIsReady()
		{
			bool isPlaying = Application.isPlaying;
			bool result;
			if (isPlaying)
			{
				EditorUtility.DisplayDialog("提示", "请停止Play再执行扫描。", "确认");
				result = false;
			}
			else
			{
				bool flag = !ApiCompatibilityUtils.Instance.Ready();
				if (flag)
				{
					EditorUtility.DisplayDialog("提示", "请确保工程没有编译报错再执行扫描", "确认");
					result = false;
				}
				else
				{
					result = true;
				}
			}
			return result;
		}

		// Token: 0x04000AF2 RID: 2802
		private const string ScanStartTime = "ScanStartTime";

		// Token: 0x04000AF3 RID: 2803
		private StreamWriter streamWriter_rules;

		// Token: 0x04000AF4 RID: 2804
		private StreamWriter streamWriter_thres;

		// Token: 0x04000AF5 RID: 2805
		private GlobalConfig globalConfig;

		// Token: 0x04000AF6 RID: 2806
		private string modulesToRunPrefsKey = "ModulesToRun";
	}
}
